import { AlertController } from 'ionic-angular';




// import { TabsPage } from './../pages/tabs/tabs';
import { Result } from "./../model/Result.d";
import { WelcomePage } from "./../pages/welcome/welcome";
import { Storage } from "@ionic/storage";
import { Component, Injector, ViewChild } from "@angular/core";
import {
  Platform,
  App,
  ToastController,
  Nav,
  NavController
} from "ionic-angular";
import { StatusBar } from "@ionic-native/status-bar";
import { SplashScreen } from "@ionic-native/splash-screen";
import { ScreenOrientation } from "@ionic-native/screen-orientation";
import { TabsPage } from "../pages/tabs/tabs";

import { HomePage } from "../pages/home/home";
import { AboutPage } from "../pages/about/about";
import { ShopSearchPage } from "../pages/shop-search/shop-search";
import { ContactPage } from "../pages/contact/contact";
import { PersonalPage } from "../pages/personal/personal";
// import { LoginPage } from '../pages/login /login';
import { LoginPage } from "./../pages/login/login";
@Component({
  templateUrl: "app.html",
  // providers: [NativeService]
  // template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
  userinfo: Promise<any>;
  checkPage: boolean;
  registerBackButton: any;

  // @ViewChild("myTabs") nav: NavController;
  @ViewChild('myNav') nav: Nav;

  // @ViewChild('myTabs') tabRef: Tabs;
  backButtonPressed: any;
  rootPage: any = TabsPage;
  result: boolean = false;
  constructor(
    public platform: Platform,
    public statusBar: StatusBar,
    public splashScreen: SplashScreen,
    public alertCtrl: AlertController,
    private screenOrientation: ScreenOrientation,
    private app: App,
    public toastCtrl: ToastController,
    // private navCtrl: NavController,
    // public appCtrl: App,
    // public toastCtrl: ToastController,
    // public viewCtrl: ViewController,
    private storage: Storage,
    // public jPush: JPush
  ) {
    //通过key，判断曾经师傅进入过引导页
    this.storage.get("firstIn").then(result => {
      console.log("firstIn is", result);
      if (result) {
        this.rootPage = TabsPage;
      } else {
        this.rootPage = WelcomePage;
        this.storage.set("firstIn", true);
      }
    });

    /* 极光推送*/
    // this.jPush.getRegistrationID().then(regid => {
    //   console.log(regid);
    // });

    platform.ready().then(() => {
      // this.registerBackEvent();
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
      screenOrientation
        .lock(this.screenOrientation.ORIENTATIONS.PORTRAIT_PRIMARY)
        .catch(function () {
          //alert("浏览器不支持")
        });
      // this.registerBackButtonAction(null);
    });
    this.userinfo = this.storage.get("userinfo");
    console.log(this.userinfo);
  }

  // exitApp() {
  //   if (this.registerBackButton) {
  //     this.platform.exitApp();
  //   } else {
  //     this.registerBackButton = true;
  //     this.toastCtrl
  //       .create({
  //         message: "再按一次退出应用",
  //         duration: 2000,
  //         position: "top",
  //         cssClass: "toast-black"
  //       })
  //       .present();
  //     setTimeout(() => (this.registerBackButton = false), 2000); //2秒内没有再次点击返回则将触发标志标记为false
  //   }
  // }

  // // 判断当前页面
  // goBackLogic() {
  //   var currentCmp = this.app.getActiveNav().getActive().component;
    
  //   var isPage1 = currentCmp === HomePage;
  //   var isPage2 = currentCmp === AboutPage;
  //   var isPage3 = currentCmp === ShopSearchPage;
  //   var isPage4 = currentCmp === ContactPage;
  //   var isPage5 = currentCmp === PersonalPage;
  //   var isPage6 = currentCmp === LoginPage;
  
  //   console.log(currentCmp);
  //   console.log(isPage1);
  //   if (isPage1 || isPage2 || isPage3 || isPage4 || isPage5) {
  //     this.checkPage = true;
  //   } else if (isPage6) {


  //     // let confirm = this.alertCtrl.create({
  //     //   title: '提示',
  //     //   message: '是否确认退出?',
  //     //   buttons: [
  //     //     {
  //     //       text: '取消',
  //     //       handler: () => {
  //     //         this.nav.setRoot(TabsPage);
  //     //       }
  //     //     },
  //     //     {
  //     //       text: '同意',
  //     //       handler: () => {
  //     //         // this.exitApp();
  //     //         this.platform.exitApp();
  //     //       }
  //     //     }
  //     //   ]
  //     // });
  //     // confirm.present();


  //     this.exitApp();
  //   } else if (this.userinfo == null) {
  //     // this.app.getRootNav().push(TabsPage)
  //     // this.navCtrl.setRoot(TabsPage)
  //     this.nav.setRoot(TabsPage);
  //   } else {
  //     this.checkPage = false;
  //   }
  // }

  // registerBackEvent() {
  //   this.platform.registerBackButtonAction(() => {
  //     this.goBackLogic();
  //     console.log("监听右键Boolean值：" + this.checkPage);
  //     if (this.checkPage) {
  //       //如果是根目则按照需求1处理
  //       this.exitApp();
  //     } else {
  //       //非根目录返回上一级页面
  //       this.app.goBack();
  //     }
  //   }, 10);
  // }

  //注册方法
  // registerBackButtonAction(tabRef: Tabs): void {

  //   //registerBackButtonAction是系统自带的方法
  //   // console.log(this.platform.platforms());
  //   this.platform.registerBackButtonAction(() => {
  //     //获取NavController
  //     let getActiveNavs: NavController = this.appCtrl.getActiveNav();
  //     // 获取modalController     enableBack()
  //     // let getActiveModals: ModalController = this.modalCtrl.getContent();
  //     //如果可以返回上一页，则执行pop
  //     console.log(getActiveNavs)
  //     console.log(tabRef._selectHistory)
  //     console.log(6666)
  //     console.log(this.appCtrl.getRootNav())
  //     if (getActiveNavs.canGoBack()) {
  //       console.log(5555)
  //       // this.viewCtrl.dismiss();
  //       getActiveNavs.pop();
  //       // this.navCtrl.pop();
  //     } else {
  //       this.viewCtrl.dismiss();
  //     };
  //     if (tabRef == null || tabRef._selectHistory == null || tabRef._selectHistory[tabRef._selectHistory.length - 1] === tabRef.getByIndex(0).id) {
  //       console.log(8888);
  //       this.navCtrl.setRoot(TabsPage);
  //       // 选择首页第一个的标签
  //       // tabRef.select(0);
  //     } else {//if ( tabRef._selectHistory[tabRef._selectHistory.length - 1] === tabRef.getByIndex(0).id)
  //       // this.viewCtrl.dismiss();
  //       // this.appCtrl.getRootNav().push(SecondPage)
  //       this.showExit();
  //     }
  //   });
  // }

  //退出应用方法
  // private showExit(): void {
  //   //如果为true，退出
  //   if (this.backButtonPressed) {
  //     this.platform.exitApp();
  //   } else {
  //     // 第一次按，弹出Toast
  //     this.toastCtrl.create({
  //       message: '再按一次退出应用',
  //       duration: 2000,
  //       position: 'top'
  //     }).present();
  //     // this.viewCtrl.dismiss();
  //     // this.navCtrl.setRoot(TabsPage);
  //     // this.registerBackButtonAction(null);

  //     //标记为true
  //     this.backButtonPressed = true;
  //     //两秒后标记为false，如果退出的话，就不会执行了
  //     setTimeout(() => this.backButtonPressed = false, 2000);
  //   }
  // }
}
